<?php
namespace app\savant\controller;

use think\Controller;
use app\common\model\Savant;
use app\common\utils\ResultVOUtil;
use app\common\enums\ResultEnum;
use app\common\exception\ZeduException;

/** 
 * @author zi-m.cn
 *  专家登录控制器
 */
class Login extends Controller
{
    public function login($userName='',$password=''){
        if($userName && $password){
            $model = Savant::where(['user_name'=>$userName])->field('id,pid,password,thumb,nick_name,last_login,last_ip,status')->find();
            //var_dump($model);
            if($model == null){
                return ResultVOUtil::error(ResultEnum::LOGIN_FAIL['code'],'用户名不存在');
            }
            if($model['status'] != 1){
                return ResultVOUtil::error(ResultEnum::LOGIN_FAIL['code'],'用户被禁用');
            }
            if(!password_verify($password,$model['password'])){
                return ResultVOUtil::error(ResultEnum::LOGIN_FAIL['code'],'密码不正确');
            }
            
            $login['id'] = $model['id'];
            $login['last_ip'] = $model['last_ip'];
            $login['last_login'] = $model['last_login'];
            $login['nick_name'] = $model['nick_name'];
            session('savant',$login);
            if($model['pid']==0){
                session('savant.pid',$model['id']);
            }else {
                session('savant.pid',$model['pid']);
            }
            return ResultVOUtil::success($login);
            
        }else{
            throw new ZeduException(ResultEnum::PARAM_ERROR); 
        }
    }
}

